package com.rrjjg.app.admin.api;

import com.rrjjg.app.admin.api.vo.PlatformVo;
import com.rrjjg.app.admin.bus.BPlatformService;
import com.rrjjg.app.admin.bus.bo.PlatformBo;
import com.rrjjg.app.admin.bus.bo.TreeBo;
import com.rrjjg.app.admin.data.storage.entity.Platform;
import com.rrjjg.app.core.beans.ResultData;
import com.rrjjg.app.core.utils.ValidationUtil;
import com.rrjjg.app.mybatis.bean.PageData;
import com.rrjjg.app.mybatis.bean.PageVo;
import com.rrjjg.app.mybatis.util.PageUtil;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * <p>
 * 平台 前端控制器
 * </p>
 *
 * @author vayne
 * @since 2023-04-06
 */
@RestController
@RequestMapping("/rrjjg/platform")
public class PlatformController {

    @Autowired
    BPlatformService bPlatformService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResultData<Boolean> add(@RequestBody PlatformVo vo) {
        ValidationUtil.validation(vo);
        boolean save = bPlatformService.getiPlatformService().save(PageUtil.transformation(vo,Platform::new));
        return new ResultData<>(save);
    }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public ResultData<Boolean> del(@RequestBody PlatformVo vo) {
        ValidationUtil.validation(vo, ValidationUtil.del.class);
        boolean rem = bPlatformService.getiPlatformService().removeById(vo.getId());
        return new ResultData<>(rem);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResultData<Boolean> update(@RequestBody PlatformVo vo) {
        ValidationUtil.validation(vo, ValidationUtil.update.class);
        boolean save = bPlatformService.getiPlatformService().updateById(PageUtil.transformation(vo,Platform::new));
        return new ResultData<>(save);
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ResultData<PageData<Platform>> list(@RequestBody PageVo<PlatformVo> vo) {
        PlatformVo data = vo.getData();
        val platformPage = bPlatformService.queryPage(PageUtil.transformation(data, Platform::new), vo.getPage(), vo.getSize());
        return new ResultData<>(platformPage);
    }


    /*获取平台数据*/
    @RequestMapping(value = "/platformList", method = RequestMethod.GET)
    public ResultData<List<Platform>> platformList() {
        val platformPage = bPlatformService.getiPlatformService().getPlatform();
        return new ResultData<>(platformPage);
    }

    /*平台下服务列表*/
    @RequestMapping(value = "/serviceList", method = RequestMethod.POST)
    public ResultData<PageData<PlatformBo>> serviceList(@RequestBody PageVo<PlatformVo> vo) {
        PlatformVo data = vo.getData();
        val platformPage = bPlatformService.serviceList(PageUtil.transformation(data, Platform::new), vo.getPage(), vo.getSize());
        return new ResultData<>(platformPage);
    }


    /*获取服务树形结构*/
    @RequestMapping(value = "/listTree", method = RequestMethod.GET)
    public ResultData<List<TreeBo>> listTree() {
        val treeDto = bPlatformService.tree();
        return new ResultData<>(treeDto);
    }

}
